package com.zhenhe.core.common.utils.excel;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;


public class ExcelUtil {

	/**
	 * 导出excel
	 * 
	 * @param excelHead
	 *            excel标题
	 * @param dataList
	 *            数据
	 * @param FileName
	 *            文件名
	 * @param SheetName
	 *            sheet名
	 */
	public static Map<String, Object> exportExcel(HttpServletResponse response,
			List<String> excelHead, List<List<String>> dataList,
			String FileName, String SheetName,String titleName) {
		try {
			ServletOutputStream out = response.getOutputStream();
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String(FileName.getBytes("GB2312"), "8859_1")
					+ ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型
			// 建立excel文件
			HSSFWorkbook workBook = new HSSFWorkbook();
			Integer totalCount = dataList.size();
			for (int n = 0; n < (totalCount / 10000) + 1; n++) {
				HSSFSheet sheet;
				if (n == 0) {
					// 创建一个工作表
					sheet = workBook.createSheet(SheetName);
				} else {
					sheet = workBook.createSheet(SheetName + "(" + n + ")");
				}

				sheet.setDefaultColumnWidth(20);  
				
				 CellStyle cellStyle = workBook.createCellStyle();
				 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中
				 HSSFFont font = workBook.createFont();
				 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				 
				 
				HSSFRow row2 = sheet.createRow(0);
				sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, excelHead.size()-1));
				HSSFCell cell2 = row2.createCell(0);
				cell2.setCellValue(titleName);
				cell2.setCellStyle(cellStyle);
				// 插入文件头
				if (excelHead != null && excelHead.size() > 0) {
					HSSFRow row = sheet.createRow(1);
					for (int i = 0; i < excelHead.size(); i++) {
						// 在Label对象的子对象中指明单元格的位置和内容
						HSSFCell cell = row.createCell(i);
						cell.setCellValue(excelHead.get(i));
					}
				}

				// 插入数据
				if (dataList != null && dataList.size() > 0) {
					Integer dataLen = dataList.size() - 10000 * n;
					if (dataLen > 10000) {
						dataLen = 10000;
					}
					for (int i = 0; i < dataLen; i++) {
						List<String> lineData = dataList.get(i + 10000 * n);
						Row row = sheet.createRow(i + 2);
						for (int j = 0; j < lineData.size(); j++) {
							if (lineData.get(j) != null) {
								Cell cell = row.createCell(j);
								cell.setCellValue(lineData.get(j));
							}
						}
					}
				}
			}
			workBook.write(out);
			out.close();

			// HSSFWorkbook wb = new HSSFWorkbook(new
			// FileInputStream(inputPath));
			// // 设置密 码 保 护 ·
			// wb.writeProtectWorkbook("123456", "");
			// // 写入excel文件
			// FileOutputStream fileOut = new FileOutputStream(inputPath);
			// wb.write(fileOut);
			// fileOut.close();

			// Map<String, Object> map = new HashMap<String, Object>();
			// map.put("fileName", FileName);
			// map.put("inputPath", inputPath);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
